using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using System.IO;


public partial class Utils_ImageGallery : SFPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FillDataList("");
        }
    }
    protected override void OnPreInit(EventArgs e)
    {
        
    }
    private void FillDataList(string newFile)
    {

        DataTable insDataTableImages = new DataTable();
        insDataTableImages.Columns.Add("ImageSource", typeof(string));
        insDataTableImages.Columns.Add("Width", typeof(string));
        insDataTableImages.Columns.Add("Height", typeof(string));
        foreach (string s in Directory.GetFiles(Server.MapPath(System.Web.HttpUtility.UrlDecode( Request.QueryString["Path"]))))
        {
            if (s.ToLower().EndsWith("gif") || s.ToLower().EndsWith("jpg") || s.ToLower().EndsWith("png"))
            {
                System.Drawing.Image b = System.Drawing.Image.FromFile(s);
                insDataTableImages.Rows.Add(new object[] { System.Web.HttpUtility.UrlDecode(Request.QueryString["Path"]) + s.Replace(Server.MapPath(System.Web.HttpUtility.UrlDecode(Request.QueryString["Path"])), ""), b.Width.ToString(), b.Height.ToString() });
                b.Dispose();
            }
            
        }
        dltPictures.DataSource = insDataTableImages;
        dltPictures.DataBind();

        foreach (DataListItem dli in dltPictures.Items)
        {
            ((Image)dli.FindControl("ibtPreview")).Attributes.Add("onclick", "hdnImage.value=this.src;document.getElementById('imgPreview').src=this.src;document.getElementById('txtWidth').value=this.OrgWidth;document.getElementById('txtHeight').value=this.OrgHeight;");
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (flUpload.PostedFile.FileName != "")
        {
            Guid g = Guid.NewGuid();
            string path = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpUtility.UrlDecode(Request.QueryString["Path"])) + g.ToString() + flUpload.FileName;
            flUpload.SaveAs(path);

            DataTable insDataTableImages = new DataTable();
            insDataTableImages.Columns.Add("ImageSource", typeof(string));
            insDataTableImages.Columns.Add("Width", typeof(string));
            insDataTableImages.Columns.Add("Height", typeof(string));

            System.Drawing.Image b = System.Drawing.Image.FromFile(path);
            insDataTableImages.Rows.Add(new object[] { System.Web.HttpUtility.UrlDecode(Request.QueryString["Path"]) + path.Replace(Server.MapPath(System.Web.HttpUtility.UrlDecode(Request.QueryString["Path"])), ""), b.Width.ToString(), b.Height.ToString() });
            b.Dispose();
            dltPictures.DataSource = insDataTableImages;
            dltPictures.DataBind();
            foreach (DataListItem dli in dltPictures.Items)
            {
                ((Image)dli.FindControl("ibtPreview")).Attributes.Add("onclick", "hdnImage.value=this.src;document.getElementById('imgPreview').src=this.src;document.getElementById('txtWidth').value=this.OrgWidth;document.getElementById('txtHeight').value=this.OrgHeight;");
            }
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "cls", "window.returnValue='" + hdnImage.Value + "|" + txtWidth.Text + "|" + txtHeight.Text + "';window.close();", true);
    }
}
